<?php
// +----------------------------------------------------------------------
// | my
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: $this <498944516@qq.com>
// +----------------------------------------------------------------------
namespace app\rob\model;

use think\Model;

class RobMore extends Model
{

    protected $table = "my_rob_more";

    /**
     * 功能集
     * TODO::追加
     * 随机获取一个机器人功能
     * */
    public function info($id,$data=[]){

      $more = $this->get($id);
     
      switch ($more['type']) {
      	case '1':  //随机
      		$map['id']=['in',$more['answers']];
      		$answers=db('RobAnswer')->where($map)->select();
      		//判断性别
      		foreach ($answers as $key => $answer) {
      			if ($answer['for_sex'] and $answer['for_sex']<>$data['fans']['sex']) unset($answers[$key]);
      		}
      		//从新编号键值
      		$answers=array_values($answers);
      		//随机发送
      		$luck=rand(0,count($answers)-1);
      		$reply=model('rob/RobAnswer')->infoByMap(['id'=>$answers[$luck]['id']],$data);
      		
      		break;

        case '2':  //随机
          $map['id']=['in',$more['answers']];
          $answers=db('RobAnswer')->where($map)->select();
          
          $ok=1;
          foreach ($answers as $key => $answer) {
            //判断性别
            if ($answer['for_sex'] and $answer['for_sex']<>$data['fans']['sex']) continue;
            if ($ok==1)$reply=model('rob/RobAnswer')->infoByMap(['id'=>$answer['id']],$data);
            if ($ok==2)customMessage($data['fans']['openid'],$answer['id']);
            $ok=2;
            
          }
          
          break;

      
      	
      	default:
      		# code...
      		break;
      }

     

      return $reply;
    		
     
    }


   
}